// pages/roleProjectDetail/roleProjectDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    zhezhao: false,
    acceptTipWindow: false,
    projectData: {},
    showStatusTipBar: true,
    statusTipBarText: '',
  },
  accept: function(e) {
    // 点击接受
    let that = this
    let config = wx.getStorageSync('config')
    let agreeId = e.currentTarget.dataset.projectagreeid
    wx.request({
      url: config.host + 'api/productCheckIn/projectAgree',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        projectAgreeId: agreeId,
        status: 1
      },
      success(res) {
        if (res.data.status == 200) {
          that.setData({
            zhezhao: !that.data.zhezhao,
            acceptTipWindow: !that.data.acceptTipWindow
          })
          // 刷新数据
          that.getProjectData(that.data.projectData.projectDTOList.projectId)
          setTimeout(() => {
            that.setData({
              zhezhao: !that.data.zhezhao,
              acceptTipWindow: !that.data.acceptTipWindow
            })
          }, 800)
        }
      }
    })
  },
  refuse: function(e) {
    // 点击拒绝
    let config = wx.getStorageSync('config')
    let agreeId = e.currentTarget.dataset.projectagreeid

    wx.showModal({
      content: '确定拒绝该项目吗？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: config.host + 'api/productCheckIn/projectAgree',
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              projectAgreeId: agreeId,
              status: 2
            },
            success(res) {
              if (res.data.status == 200) {
                wx.showToast({
                  title: '拒绝成功',
                  icon: 'none'
                })
                // 刷新数据
                that.getProjectData(that.data.projectData.projectDTOList.projectId)
              }
            }
          })
        } else if (res.cancel) {
          console.log('取消拒绝')
        }
      }
    })
  },
  getProjectData: function(projectId) {
    // 获取项目详情
    let that = this
    let config = wx.getStorageSync('config')
    let productId = wx.getStorageSync('productId')
    wx.request({
      url: config.host + 'api/productCheckIn/findProjectInfo?projectId=' + projectId + '&productId=' + productId,
      method: 'GET',
      success(res) {
        if (res.data.status == 200) {
          let data = res.data.data
          data.showStatusTipBar = true
          if (data.projectStatus == 0) {
            if (data.agreeStatus == 0) {
              data.statusTipBarText = '未处理'
              data.showStatusTipBar=false
            } else if (data.agreeStatus == 1) {
              data.statusTipBarText = '已接受'
            } else if (data.agreeStatus == 2) {
              data.statusTipBarText = '已拒绝'
            } else if (data.agreeStatus == 3) {
              data.statusTipBarText = '已暂停'
            } else if (data.agreeStatus == 4) {
              data.statusTipBarText = '你已被移出该项目'
            } else if (data.agreeStatus == 5) {
              data.statusTipBarText = '项目已终止'
            }
          } else if (data.projectStatus == 1) {
            data.statusTipBarText = '已过期'
          } else if (data.projectStatus == 2) {
            data.statusTipBarText = '已暂停'
          } else if (data.projectStatus == 3) {
            data.statusTipBarText = '已终止'
          }
          that.setData({
            projectData: data
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 获取项目详情
    let projectId = options.projectId
    this.getProjectData(projectId)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})